<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceEventList extends core {

    public function process() {
        $limit = $this->limit;
        $page = intval($_POST['page']);
        $user_id = intval($_POST['user_id']);
        if ($page < 1) {
            $page = 1;
        }
        $from = ($page - 1) * $limit;
        $total = 0;

        $data = $this->db->get_results("SELECT * FROM events WHERE user_id={$user_id} ORDER BY start_time asc LIMIT $from,$limit;");
        $total = $this->db->get_row("SELECT count(event_id) as total FROM events WHERE user_id={$user_id} ;");

        if (ceil($total->total / $limit) <= $page) {
            $end_paging = 1;
        } else {
            $end_paging = 0;
        }

        $html = '';
        foreach ($data as $event) {
            $total_guest = $this->db->get_row("SELECT count(guest_id) as total FROM guest_event WHERE event_id=$event->event_id");
            if (($event->start_time != "0000-00-00 00:00:00") && ($event->end_time != "0000-00-00 00:00:00")) {
                if ($event->end_time == $event->start_date) {
                    $time = date('h:i A', strtotime($event->start_time));
                } else {
                    $time = date('h:i A', strtotime($event->start_time)) . ' - ' . date('h:i A', strtotime($event->end_time));
                }
            } else {
                $time = '';
            }
            $html.='<li class="wd-list-event-item" >
                                <div class="wd-list-event-item-right">
                                    <span class="wd-list-event-item-txt-title">' . date('m/d/Y', strtotime($event->start_date)) . '</span>
                                    <span class="wd-list-event-item-txt-des">' . $time . '</span>
                                    <div class="wd-list-event-item-right-grp-action">
                                        <a href="eventDetail.html" class="wd-edit-guest" rel="' . $event->event_id . '" ><img src="img/edit-icon.png"></a>';
                                        if($event->canbedeleted==1){
                                        $html.='<span class="wd-delete-guest" rel="' . $event->event_id . '"><img src="img/delete-icon.png"></span>';
                                        }
                                    $html.='</div>
                                </div>
                                <div class="wd-list-event-item-left">
                                    <a href="eventDetail.html" class="wd-list-event-item-txt-title">' . $event->title . '</a>
                                    <span class="wd-list-event-item-txt-des">' . $event->location . '</span>
                                    <span class="wd-list-event-item-txt-des">' . nl2br($event->description) . '</span>
                                    <div class="wd-list-event-item-left-grp-action">
                                        <span class="wd-list-event-act-add" rel="' . $event->event_id . '">
                                            <img src="img/add-icon-act.png">
                                            <span class="wd-list-event-act-txt">Add Guest</span>
                                        </span>';
            if ($total_guest->total > 0) {
                $html.='  <span class="wd-list-event-act-adress btn-event-act-adress"  rel="' . $event->event_id . '">
                                            <img src="img/requestadress-icon-act.png">
                                            <span class="wd-list-event-act-txt">Request Address</span>
                                        </span>';
                $html .= '<span class="wd-list-event-act-adress wd-list-event-act-email"  rel="' . $event->event_id . '">
                                            <img src="img/email-ico-color.png">
                                            <span class="wd-list-event-act-txt">Email Guest List</span>
                                        </span>';
                if ($event->start_time == "0000-00-00 00:00:00") {
                    $html.=' <span class="wd-list-event-act-adress wd-list-event-act-invite" rev="'.$event->title.'" rel="' . $event->event_id . '">
                                            <img src="img/invite-icon.png">
                                            <span class="wd-list-event-act-txt">Invite Guests </span>
                                        </span>';
                }else{
                    $html.=' <span class="wd-list-event-act-adress wd-list-event-act-invite"  rel="' . $event->event_id . '">
                                            <img src="img/invite-icon.png">
                                            <span class="wd-list-event-act-txt">Invite Guests </span>
                                        </span>';
                }
                         
            }
            
            $html.='                       </div>
                                </div>
                            </li>';
        }
        if ($html == '') {
            $html = '<li class="wd-list-event-item" >No events found. Touch the Add button to create a new one.</li>';
        }
        $this->_JSONResponse(array('error' => 0, 'html' => $html, 'page' => $page + 1, 'end_paging' => $end_paging));
    }

}

$processor = new webServiceEventList();
$processor->process();
?>